package book.action;

import static common.utils.Constants.LIMIT;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.impl.BookTypeServiceImpl;

import common.action.BaseAction;
import common.pojo.BookType;
import common.utils.Constants;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;
import common.utils.SessionUtils;

@Scope("prototype")
@Controller(value = "bookType")
public class BookTypeAction extends BaseAction {

	/**
	 * 获取列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public void getTypeLists() throws Exception {
		bTypelist =bTypeService.find();
		JSONArray array = JSONArray.fromObject(bTypelist);
		response.getWriter().write("{\"rows\":"+array.toString()+"}");
	}
	
	/**
	 * 获取列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getBookTypeListByPage() throws Exception {
		String currentTmp = request.getParameter("current");//获取页码参数
		BookType bType = new BookType();
		bType.setDeleted(Constants.NOT_DELETED);
		int total= bTypeService.queryByCount(bType);//查询符合条件的类型数量
		int current = PageUtil.getCurrent(currentTmp);//判断当前页码数
		Page page = PageUtil.createPage(LIMIT, total, current);//生成页码实体参数
		bTypelist = bTypeService.find(page.getBeginIndex(), LIMIT);//根据页码参数以及每页显示数量获取列表
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getBookTypeListByPage.action","");//页码显示的静态代码，字符串，很长
		return "bTypeList";
	}


	/**
	 * 删除
	 */
	public String deleteById() {
		String ids = request.getParameter("ids");
		bTypeService.delete(ids,SessionUtils.getAdminName(request));
		return SUCCESS;
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String add() {
		bTypeService.add(bType);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return "success2";
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String update() {
		bType.setUpdateName(SessionUtils.getAdminName(request));
		bType.setUpdateTime(new Date());
		bTypeService.update(bType);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return "success2";
	}
	
	/**
	 * 按id查询
	 * @return
	 */
	public String findInfoById() {
		String id = request.getParameter("id");
		bType=bTypeService.findInfoById(id);
		return "info";
	}
	private static final long serialVersionUID = 1L;

	//引用service层
	@Resource
	private BookTypeServiceImpl bTypeService;
	private BookType bType;
	private List<BookType> bTypelist;//列表
	private String pagination;//分页
	private String dofunction;//返回处理刷新

	
	public BookType getbType() {
		return bType;
	}


	public void setbType(BookType bType) {
		this.bType = bType;
	}


	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	
	public List<BookType> getbTypelist() {
		return bTypelist;
	}

	public void setbTypelist(List<BookType> bTypelist) {
		this.bTypelist = bTypelist;
	}



}
